using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;
using System.Collections;
using Widgetsphere.Core.Exceptions;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.Business;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Business.Rules;
using ACME.Northwind.Business.SelectCommands;
using System.Collections.Generic;

namespace ACME.Northwind.Domain.Objects
{
	/// <summary>
	/// This is an customizable extender for the domain class associated with the 'Product' object
	/// </summary>
	[Serializable()]
	partial class DomainProduct : PersistableDomainObjectBase
	{
		#region Member Variables

		private const string TABLE_NAME = "ProductCollection";
		internal DomainProductCollection ParentCol;

		#endregion

		#region Constructor

		internal DomainProduct(DataRowBuilder rb) : base(rb) 
		{
			this.ParentCol = ((DomainProductCollection)(base.Table));
		}

		#endregion

		#region Properties

		internal ProductPrimaryKey PrimaryKey
		{
			get { return new ProductPrimaryKey(this.ProductId); }
		}

		internal int? CategoryId
		{
			get
			{
				try
				{
					if (base[this.ParentCol.CategoryIdColumn] == System.DBNull.Value) return null;
					else return ((int?)(base[this.ParentCol.CategoryIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.CategoryIdColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.CategoryIdColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ProductCollection list = (ProductCollection)this.ParentCol.SubDomain[Collections.ProductCollection];
					Product item = list.GetItemByPK(this.ProductId);
					if (item != null) item.CategoryId = value;
				}
			}
		}

		internal bool Discontinued
		{
			get
			{
				try
				{
					return ((bool)(base[this.ParentCol.DiscontinuedColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  if (!base[this.ParentCol.DiscontinuedColumn].Equals(value))
				{
					base[this.ParentCol.DiscontinuedColumn] = value;
			  	wasSet = true;
				}

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ProductCollection list = (ProductCollection)this.ParentCol.SubDomain[Collections.ProductCollection];
					Product item = list.GetItemByPK(this.ProductId);
					if (item != null) item.Discontinued = value;
				}
			}
		}

		internal int ProductId
		{
			get
			{
				try
				{
					return ((int)(base[this.ParentCol.ProductIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
		}

		internal string ProductName
		{
			get
			{
				try
				{
					if (base[this.ParentCol.ProductNameColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.ProductNameColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  if (value == null) throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_SETNULL);

			  if (!base[this.ParentCol.ProductNameColumn].Equals(value))
				{
					base[this.ParentCol.ProductNameColumn] = value;
			  	wasSet = true;
				}

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ProductCollection list = (ProductCollection)this.ParentCol.SubDomain[Collections.ProductCollection];
					Product item = list.GetItemByPK(this.ProductId);
					if (item != null) item.ProductName = value;
				}
			}
		}

		internal string QuantityPerUnit
		{
			get
			{
				try
				{
					if (base[this.ParentCol.QuantityPerUnitColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.QuantityPerUnitColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.QuantityPerUnitColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.QuantityPerUnitColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ProductCollection list = (ProductCollection)this.ParentCol.SubDomain[Collections.ProductCollection];
					Product item = list.GetItemByPK(this.ProductId);
					if (item != null) item.QuantityPerUnit = value;
				}
			}
		}

		internal short? ReorderLevel
		{
			get
			{
				try
				{
					if (base[this.ParentCol.ReorderLevelColumn] == System.DBNull.Value) return null;
					else return ((short?)(base[this.ParentCol.ReorderLevelColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.ReorderLevelColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.ReorderLevelColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ProductCollection list = (ProductCollection)this.ParentCol.SubDomain[Collections.ProductCollection];
					Product item = list.GetItemByPK(this.ProductId);
					if (item != null) item.ReorderLevel = value;
				}
			}
		}

		internal int? SupplierId
		{
			get
			{
				try
				{
					if (base[this.ParentCol.SupplierIdColumn] == System.DBNull.Value) return null;
					else return ((int?)(base[this.ParentCol.SupplierIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.SupplierIdColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.SupplierIdColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ProductCollection list = (ProductCollection)this.ParentCol.SubDomain[Collections.ProductCollection];
					Product item = list.GetItemByPK(this.ProductId);
					if (item != null) item.SupplierId = value;
				}
			}
		}

		internal decimal? UnitPrice
		{
			get
			{
				try
				{
					if (base[this.ParentCol.UnitPriceColumn] == System.DBNull.Value) return null;
					else return ((decimal?)(base[this.ParentCol.UnitPriceColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.UnitPriceColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.UnitPriceColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ProductCollection list = (ProductCollection)this.ParentCol.SubDomain[Collections.ProductCollection];
					Product item = list.GetItemByPK(this.ProductId);
					if (item != null) item.UnitPrice = value;
				}
			}
		}

		internal short? UnitsInStock
		{
			get
			{
				try
				{
					if (base[this.ParentCol.UnitsInStockColumn] == System.DBNull.Value) return null;
					else return ((short?)(base[this.ParentCol.UnitsInStockColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.UnitsInStockColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.UnitsInStockColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ProductCollection list = (ProductCollection)this.ParentCol.SubDomain[Collections.ProductCollection];
					Product item = list.GetItemByPK(this.ProductId);
					if (item != null) item.UnitsInStock = value;
				}
			}
		}

		internal short? UnitsOnOrder
		{
			get
			{
				try
				{
					if (base[this.ParentCol.UnitsOnOrderColumn] == System.DBNull.Value) return null;
					else return ((short?)(base[this.ParentCol.UnitsOnOrderColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.UnitsOnOrderColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.UnitsOnOrderColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ProductCollection list = (ProductCollection)this.ParentCol.SubDomain[Collections.ProductCollection];
					Product item = list.GetItemByPK(this.ProductId);
					if (item != null) item.UnitsOnOrder = value;
				}
			}
		}

		internal virtual BusinessObjectList<OrderDetail> OrderDetailList
		{
			get
			{
				BusinessObjectList<OrderDetail> associatedOrderDetailList = null;
				//if (associatedOrderDetailList == null)
				//{
					associatedOrderDetailList = new BusinessObjectList<OrderDetail>();
					foreach (DomainOrderDetail OrderDetail in base.GetChildRows(ParentCol.ProductOrderDetailRelation))
					{
						if (OrderDetail.RowState != DataRowState.Deleted)
							associatedOrderDetailList.Add(new OrderDetail(OrderDetail));
					}
					associatedOrderDetailList.ObjectAdded += new BusinessObjectEventHandler(AssociatedOrderDetailList_ObjectAdded);
					associatedOrderDetailList.ObjectRemoved += new BusinessObjectEventHandler(AssociatedOrderDetailList_ObjectRemoved);
				//}
				return associatedOrderDetailList;
			}
		}

		internal virtual DomainCategory CategoryItem
		{
			get { return (DomainCategory)base.GetParentRow(this.ParentCol.CategoryProductRelation); }
			set
			{
				if (value != null)
				{
					DomainCategoryCollection list = null;
					if (value.GetType() == typeof(DomainCategory))
						list = (DomainCategoryCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.CategoryCollection);
					bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
					this.ParentCol.SubDomain.EnforceConstraints = false;
					DomainCategory category = (DomainCategory)value;
					if(category.ParentCol != list)
					{
						this.ParentCol.SubDomain.Merge(new DataRow[] { category }, false, MissingSchemaAction.Error);
						category = list.GetCategory(category.CategoryId);
					}
					base.SetParentRow(category, this.ParentCol.CategoryProductRelation);
					this.ParentCol.SubDomain.EnsureParentRowsExist();				
					this.ParentCol.SubDomain.EnforceConstraints = oldEc;
				}
				else
				{
					if(this.ParentCol.CategoryIdColumn.AllowDBNull)
					{
						base.SetNull(this.ParentCol.CategoryIdColumn);
					}
					else
					{
						this.Delete();
					}
				}
			}
		}

		internal virtual DomainSupplier SupplierItem
		{
			get { return (DomainSupplier)base.GetParentRow(this.ParentCol.SupplierProductRelation); }
			set
			{
				if (value != null)
				{
					DomainSupplierCollection list = null;
					if (value.GetType() == typeof(DomainSupplier))
						list = (DomainSupplierCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.SupplierCollection);
					bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
					this.ParentCol.SubDomain.EnforceConstraints = false;
					DomainSupplier supplier = (DomainSupplier)value;
					if(supplier.ParentCol != list)
					{
						this.ParentCol.SubDomain.Merge(new DataRow[] { supplier }, false, MissingSchemaAction.Error);
						supplier = list.GetSupplier(supplier.SupplierId);
					}
					base.SetParentRow(supplier, this.ParentCol.SupplierProductRelation);
					this.ParentCol.SubDomain.EnsureParentRowsExist();				
					this.ParentCol.SubDomain.EnforceConstraints = oldEc;
				}
				else
				{
					if(this.ParentCol.SupplierIdColumn.AllowDBNull)
					{
						base.SetNull(this.ParentCol.SupplierIdColumn);
					}
					else
					{
						this.Delete();
					}
				}
			}
		}

		#endregion

		#region Collection Operation Methods

		/// <summary>
		/// Serves as a hash function for this particular type.
		/// </summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null) return false;
			if (obj.GetType() != this.GetType()) return false;

			if ((this.RowState == DataRowState.Deleted) ||
				(((DataRow)obj).RowState == DataRowState.Deleted) ||
				(this.RowState == DataRowState.Detached) ||
				(((DataRow)obj).RowState == DataRowState.Detached))
				return false;

			return ((DomainProduct)obj).PrimaryKey.Equals(this.PrimaryKey);
		}

		public override void Persist()
		{
			DomainHelper.PersistDomainObject(this, this.ParentCol.SubDomain, Collections.ProductCollection, this.ParentCol.Modifier);
		}

		public override void Remove()
		{
			this.ParentCol.RemoveProduct(this);
		}

		#endregion

		#region Relationship Methods
		private void AssociatedOrderDetailList_ObjectAdded(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainOrderDetail addedItem = (DomainOrderDetail)e.BusinessObject.WrappedClass;
			DomainOrderDetailCollection existingOrderDetailCollection = (DomainOrderDetailCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.OrderDetailCollection);
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			if(addedItem.ParentCol != existingOrderDetailCollection)
			{
				this.ParentCol.SubDomain.Merge(new DataRow[] { addedItem } , false, MissingSchemaAction.Error);
				addedItem = existingOrderDetailCollection.GetOrderDetail(addedItem.OrderId, addedItem.ProductId);
				e.BusinessObject.WrappedClass = addedItem;
			}
			addedItem.SetParentRow(this,this.ParentCol.ProductOrderDetailRelation);
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		private void AssociatedOrderDetailList_ObjectRemoved(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainOrderDetail removedItem = (DomainOrderDetail)e.BusinessObject.WrappedClass;
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			removedItem.Delete();
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		internal virtual void ReleaseNonIdentifyingRelationships()
		{
		}

		#endregion
	}
}
